﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinServer
{
    /// 测试服务端代码辅助类

    public class ServerDemoUtil
    {
        static Encoding encoding = Encoding.GetEncoding("gbk");

        HansAppServer myServer = new HansAppServer(encoding, "#", "$");

        public void Listen()
        {
            //Setup the appServer

            if (!myServer.Setup(1990)) //Setup with listening port
            {

                MessageBox.Show("Failed to setup!");
                return;
            }

            //Try to start the appServer

            if (!myServer.Start())
            {
                MessageBox.Show("Failed to start!");
                return;
            }
            myServer.NewSessionConnected += MyServer_NewSessionConnected;

            myServer.NewRequestReceived += MyServer_NewRequestReceived;

            myServer.SessionClosed += myServer_SessionClosed;

        }

        private void MyServer_NewRequestReceived(HansAppSession session, HansRequestInfo requestInfo)
        {

            string msg = requestInfo.Body.BodyString;//encoding.GetString(requestInfo.Body.BodyBuffer);

            MessageBox.Show(msg);

        }
        private void MyServer_NewSessionConnected(HansAppSession session)
        {

            session.Send("Welcome to SuperSocket Telnet Server");

        }

        void myServer_SessionClosed(HansAppSession session, SuperSocket.SocketBase.CloseReason value)
        {

            throw new NotImplementedException();

        }

    }

}

